#博客列表页
import time
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from common.Utils import BlogDriver

class BlogList:
    """
    用于测试博客列表页
    :return:
    """
    def __init__(self):
        # 博客登录页面url
        self.url = "http://8.137.19.140:9090/blog_list.html"
        self.driver = BlogDriver.driver
        self.driver.get(self.url)

    def ListTestByLogin(self):
        """
        测试博客列表页是否正常获取博客标题、博客内容等
        :return:
        """
        print("----测试博客列表页是否显示正常---")
        #获取第一篇博客标题、时间、内容
        title = self.driver.find_element(By.CSS_SELECTOR,
                                         "body > div.container > div.right > div:nth-child(1) > div.title").text
        time = self.driver.find_element(By.CSS_SELECTOR,
                                         "body > div.container > div.right > div:nth-child(1) > div.date").text
        describe = self.driver.find_element(By.CSS_SELECTOR,
                                         "body > div.container > div.right > div:nth-child(1) > div.desc").text

        # 添加屏幕截图
        BlogDriver.SaveScreenCut(name=type(self).__name__)
        print("博客标题：{},时间：{},内容：{}".format(title,time,describe))

        # 测试"查看全文按钮"是否正常
        self.driver.find_element(By.CSS_SELECTOR, "body > div.container > div.right > div:nth-child(1) > a").click()
        #添加隐式等待，等待页面加载完成
        self.driver.implicitly_wait(5)
        #判断是否正确加载
        self.driver.find_element(By.CSS_SELECTOR, "body > div.container > div.right > div > div.title")
        self.driver.find_element(By.CSS_SELECTOR, "body > div.container > div.right > div > div.date")
        self.driver.find_element(By.CSS_SELECTOR, "#detail > p")

        # 添加屏幕截图
        BlogDriver.SaveScreenCut(name=type(self).__name__)

        print("查看全文按钮正常")
        print("----测试结束----\n")

    def ButtonTest(self):
        """
        用于测试登录右上角的按钮跳转是否正常
        :return:
        """
        print("----测试博客列表页按钮功能跳转是否正常----")
        # 测试主页按钮是否正常跳转
        self.driver.find_element(By.CSS_SELECTOR, "body > div.nav > a:nth-child(4)").click()

        #添加显示等待
        WebDriverWait(self.driver, 20).until(EC.title_is("博客列表页"))

        # 添加屏幕截图
        BlogDriver.SaveScreenCut(name=type(self).__name__)
        print("主页按钮功能正常，当前页面："+"博客列表页")

        #测试写博客按钮是否跳转正常
        self.driver.find_element(By.CSS_SELECTOR, "body > div.nav > a:nth-child(5)").click()

        #添加显示等待
        WebDriverWait(self.driver, 20).until(EC.title_is("博客编辑页"))

        title = self.driver.title
        assert title == "博客编辑页"
        print("写博客按钮功能正常，当前页面：",title)

        # 添加屏幕截图
        time.sleep(3)
        BlogDriver.SaveScreenCut(name=type(self).__name__)
        print("----测试结束----\n")

    def ListTestByNotlogged(self):
        """
        用于测试未登录状态下，进入博客列表页
        :return:
        """
        print("----测试未登录状态下，进入博客列表页----")
        #先注销后打开网址
        self.driver.find_element(By.CSS_SELECTOR,"body > div.nav > a:nth-child(6)").click()
        self.driver.get("http://8.137.19.140:9090/blog_list.html")

        #添加显示等待
        WebDriverWait(self.driver, 20).until(EC.title_is("博客登陆页"))

        title = self.driver.title
        print("当前返回页面："+title)

        # 添加屏幕截图
        BlogDriver.SaveScreenCut(name=type(self).__name__)
        print("----测试结束----\n")